package com.shishuo.cms.action.manage;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.CashEntity;
import com.shishuo.cms.entity.UserEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.CashService;
import com.shishuo.cms.service.UserService;
@Controller
@RequestMapping("/admin/cash")
public class CashAction  extends AdminBaseAction {
	@Autowired
	private CashService cashService;
	@Autowired
	private UserService userService;
	@InitBinder("cashEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("cashEntity.");
	}
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", cashService.getAllListPage(pageNum));
		return "system/cash/all";
	}

	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(
			@RequestParam(value = "id", defaultValue = "0") long id,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		CashEntity cashEntity = cashService.getById(id);
		modelMap.put("cashEntity", cashEntity);
		UserEntity cashUserEntity=userService.getById(cashEntity.getUserId());
		modelMap.put("cashUserEntity", cashUserEntity);
		return "system/cash/update";
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> editAdmin(@ModelAttribute CashEntity cashEntity)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (cashEntity.getPassMsg()==null||"".equals(cashEntity.getPassMsg())) {
			json.getErrors().put("passMsg", "审核信息不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		CashEntity cashEntityOld = cashService.getById(cashEntity.getId());
		cashEntityOld.setStatus(cashEntity.getStatus());
		cashEntityOld.setPassMsg(cashEntity.getPassMsg());
		cashService.withDrawByAdmin(cashEntityOld);
		json.setResult(true);
		json.setMsg("审核成功");
		return json;
	}
}
